package org.dream.ui;

import static org.dream.ui.chart.DreamConfig.DALIANG_LINE;
import static org.dream.ui.chart.DreamConfig.DELTE_LINE;
import static org.dream.ui.chart.DreamConfig.K_LINE;
import static org.dream.ui.chart.DreamConfig.PRICE_LINE;
import static org.dream.ui.chart.DreamConfig.SHOU_LINE;
import static org.dream.ui.chart.DreamConfig.TURNINGPOINT_LINE;
import static org.dream.ui.chart.DreamConfig.TURNOVER_LINE;
import static org.dream.ui.chart.impl.KChart.C_MA_P_DAY;
import static org.dream.ui.chart.impl.KChart.C_MA_P_FLAG;
import static org.dream.ui.chart.impl.KChart.DOUIMG_P_DAY;
import static org.dream.ui.chart.impl.KChart.DOUIMG_P_FLAG;

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.dream.ui.chart.TurnOverAveLine;
import org.dream.ui.chart.impl.KChart;
import org.dream.util.PropUtil;

import com.l2fprod.common.swing.JTaskPaneGroup;

/**
 * 显示设置对话框
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class KChartSetDialog extends JDialog {
	private int x;
	private int y;
	private int value;
	private String day;
	private KChart kChart;
	private int width = 400;
	private int height = 600;
	final JCheckBox volpRadio = new JCheckBox("成交量");
	private JCheckBox jsmapRadio;
	private JCheckBox jsmap0Box;
	private JComboBox jsmapTurnoverBox;
	private JCheckBox douimgBox;
	private JSpinner douimgPDaySpinner;

	public KChartSetDialog(Frame owner, boolean modal, KChart kChart, int x, int y) {
		super(owner, null, modal);
		this.setLayout(new GridLayout(2, 1));
		this.x = x;
		this.y = y;
		this.kChart = kChart;
		this.value = (int) kChart.getXAxis().getXAxisModel().translatePxToValue(x);
		this.day = kChart.getXAxis().getXAxisModel().getTickDay(value);
		this.setTitle("时间：" + day);

		addFirWinGroup();
		addTwoWinGroup();
		jsmapTurnoverBox.setSelectedItem(KChart.JS_MA_P_TURNOVER);
		jsmapRadio.setSelected(KChart.JS_MA_P_FLAG);
		jsmap0Box.setSelected(KChart.JS_MA_P0_FLAG);
		volpRadio.setSelected(KChart.VOL_P_FLAG);
		douimgBox.setSelected(DOUIMG_P_FLAG);
		douimgPDaySpinner.setValue(DOUIMG_P_DAY);
		this.setLocation(getLocationX(), getLocationY());
		this.setSize(width, height);
		this.setVisible(true);

	}

	private int getLocationX() {
		int locationX = x;
		int rightSpace = kChart.getChartModel().getPixelXRight() - x;
		if (rightSpace < 400) {
			locationX = x - (width - rightSpace);
		}

		return locationX;
	}

	private int getLocationY() {
		int locationY = y;
		int buttomSpace = kChart.getChartModel().getPixelYButtom() - y;
		if (buttomSpace < 400) {
			locationY = y - (height - buttomSpace);
		}

		return locationY;
	}

	/** 第一窗口设置 */
	private void addFirWinGroup() {

		JTaskPaneGroup firWinGroup = new JTaskPaneGroup();
		firWinGroup.setText("第一窗口设置:");
		addDouimgGroup(firWinGroup);
		addCmapGroup(firWinGroup);
		firWinGroup.revalidate();
		firWinGroup.repaint();
		this.add(firWinGroup);
	}

	private void addDouimgGroup(JTaskPaneGroup parent) {
		JTaskPaneGroup douimgGroup = new JTaskPaneGroup();
		douimgGroup.setLayout(new FlowLayout(FlowLayout.LEFT));
		douimgGroup.setText("第二映像设置");
		douimgBox = new JCheckBox("第二映像");

		douimgGroup.add(douimgBox);
		douimgPDaySpinner = new JSpinner();
		douimgPDaySpinner.setSize(100, 10);

		douimgGroup.add(douimgPDaySpinner);
		douimgBox.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				DOUIMG_P_FLAG = douimgBox.isSelected();
				DOUIMG_P_DAY = (Integer) douimgPDaySpinner.getValue();
				kChart.refreshImage(value);
				

			}
		});
		parent.add(douimgGroup);
	}

	private void addCmapGroup(JTaskPaneGroup parent) {
		JTaskPaneGroup c_ma_p_Group = new JTaskPaneGroup();
		c_ma_p_Group.setLayout(new FlowLayout(FlowLayout.LEFT));
		c_ma_p_Group.setText("均线设置");
		final JCheckBox c_ma_p_Box = new JCheckBox("第一条均线");
		c_ma_p_Box.setSelected(C_MA_P_FLAG);
		c_ma_p_Group.add(c_ma_p_Box);
		final JSpinner firstDay = new JSpinner();
		firstDay.setSize(100, 10);
		firstDay.setValue(C_MA_P_DAY);
		c_ma_p_Group.add(firstDay);
		c_ma_p_Box.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				C_MA_P_FLAG = c_ma_p_Box.isSelected();
				C_MA_P_DAY = (Integer) firstDay.getValue();
				// kChart.clearPlot();
				if (c_ma_p_Box.isSelected()) {
					// kChart.addImage(value, IMAGE_DAY);

				}
				// kChart.repaint();

			}
		});
		parent.add(c_ma_p_Group);
	}

	/** 第二窗口设置 */
	private void addTwoWinGroup() {

		JTaskPaneGroup towWinGroup = new JTaskPaneGroup();
		towWinGroup.setText("第二窗口设置:");
		addVolpGroup(towWinGroup);
		addJsmapGroup(towWinGroup);
		towWinGroup.revalidate();
		towWinGroup.repaint();
		this.add(towWinGroup);
	}

	/**
	 * 添加上涨家数均线设置
	 * 
	 * @param parent
	 */
	private void addJsmapGroup(JTaskPaneGroup parent) {
		JTaskPaneGroup jsmapGroup = new JTaskPaneGroup();
		jsmapGroup.setLayout(new FlowLayout(FlowLayout.LEFT));
		jsmapGroup.setText("上涨家数均线设置");
		// 添加涨家数均线按钮
		jsmapRadio = new JCheckBox("大涨家均线");
		jsmapRadio.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				KChart.JS_MA_P_FLAG = jsmapRadio.isSelected();
				KChart.JS_MA_P_TURNOVER = (Integer) jsmapTurnoverBox.getSelectedItem();
				kChart.refreshJsmap();
				
			}
		});
		jsmapGroup.add(jsmapRadio);
		jsmapTurnoverBox = new JComboBox();
		for (int i = 1; i < 10; i++) {
			jsmapTurnoverBox.addItem(i);
		}

		jsmapTurnoverBox.setSize(100, 10);

		jsmapGroup.add(jsmapTurnoverBox);
		// 添加涨过0%家数线
		jsmap0Box = new JCheckBox("上涨家数均线");
		jsmap0Box.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				KChart.JS_MA_P0_FLAG = jsmap0Box.isSelected();
				if (jsmap0Box.isSelected()) {
					kChart.addJsmap0();
				} else {
					kChart.clearPlot(KChart.JS_MA_P0_ID);
				}
				kChart.repaint();

			}
		});
		jsmapGroup.add(jsmap0Box);
		parent.add(jsmapGroup);
	}

	/**
	 * 添加成交量设置
	 * 
	 * @param parent
	 */
	private void addVolpGroup(JTaskPaneGroup parent) {

		JTaskPaneGroup volpGroup = new JTaskPaneGroup();
		volpGroup.setText("成交量");
		volpGroup.setLayout(new FlowLayout(FlowLayout.LEFT));

		volpRadio.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				KChart.VOL_P_FLAG = volpRadio.isSelected();
				kChart.refreshVolp();
			}
		});

		volpGroup.add(volpRadio);

		parent.add(volpGroup);

	}


	class ViewSetPanel extends JPanel {
		JCheckBox kCheckBox = new JCheckBox("K线");
		JCheckBox shouCheckBox = new JCheckBox("收盘线");
		JCheckBox daLiangCheckBox = new JCheckBox("大量线");
		JCheckBox turningPointCheckBox = new JCheckBox("转折点");
		JCheckBox delteCheckBox = new JCheckBox("delte");
		JCheckBox turnOverBox = new JCheckBox("成交量");
		JSpinner turnOverSpinner = new JSpinner();
		JCheckBox priceBox = new JCheckBox("价格均线");
		JCheckBox compositionBox = new JCheckBox("多股对比");
		JTextField stockCodeText = new JTextField();

		public ViewSetPanel() {
			this.setLayout(new FlowLayout());
			this.add(new JLabel("K线设置:"));
			// K线
			this.addCheckItem(kCheckBox, K_LINE);
			// 收盘线
			this.addCheckItem(shouCheckBox, SHOU_LINE);
			// 大量线
			this.addCheckItem(daLiangCheckBox, DALIANG_LINE);
			// 转折点线
			this.addCheckItem(turningPointCheckBox, TURNINGPOINT_LINE);
			// delte线
			this.addCheckItem(delteCheckBox, PropUtil.getPropertyBool("delte"));
			/** 添加量线 */
			this.addCheckItem(turnOverBox, TURNOVER_LINE);

			turnOverSpinner.setSize(100, 10);
			turnOverSpinner.setValue(TurnOverAveLine.TURNOVERDAY);
			this.add(turnOverSpinner);
			turnOverSpinner.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					TurnOverAveLine.TURNOVERDAY = (Integer) turnOverSpinner.getValue();

				}
			});
			/** 添加价格均线 */
			this.add(priceBox);
			priceBox.addActionListener(new CheckActionListener());
			priceBox.setActionCommand("price");
			// 添加叠加股票
			this.add(compositionBox);
			this.add(stockCodeText);
			/** 添加第二映像 */

		}

		public void addCheckItem(JCheckBox checkBox, boolean display) {
			this.add(checkBox);
			checkBox.setSelected(display);
			checkBox.addActionListener(new CheckActionListener());
		}

		class CheckActionListener implements ActionListener {
			public void actionPerformed(ActionEvent e) {
				K_LINE = kCheckBox.isSelected();
				SHOU_LINE = shouCheckBox.isSelected();
				DALIANG_LINE = daLiangCheckBox.isSelected();
				TURNINGPOINT_LINE = turningPointCheckBox.isSelected();
				DELTE_LINE = delteCheckBox.isSelected();
				TURNOVER_LINE = turnOverBox.isSelected();
				if (e.getActionCommand().equals("price")) {
					PRICE_LINE = priceBox.isSelected();
				} else if (e.getActionCommand().equals("image")) {

				}

			}
		}

	}
}
